import store from "../../store";
import {STYLE_PREFIX, REAL_PREFIX, EDIT_PREFIX} from "../../config/components/prefix";
import {initComponentsList} from "../../helper/drag-plugin/initComponents";
import {clearDataStoreForMemory} from "../lxn/handleData";
export default function(router) {
	router.beforeEach((to, from, next) => {
		console.log('打印route',router)
		clearDataStoreForMemory();
		console.log("即将要进入的路由", from.query, to);
		if(from.query.share) {
			let ua = window.navigator.userAgent.toLowerCase();
			if(ua.match(/MicroMessenger/i) == 'micromessenger') {
				document.querySelector(".home-container").onclick = function() {
					openBrowser()
				}
			} else {
				document.querySelector(".home-container").onclick = function() {
					openApp()
				}
			}
			return
		}
		store.commit("setRouterLifeCycleOfName", {
			name: to.name
		});
		//初始化当前页面该显示哪些组件
		_initComponents(to.name);
		//清理editConfig
		store.commit("setEditConfigVuex", {});
		next();
	});
}
function _initComponents(pageName) {
	console.log("_initComponents", pageName);
	//设置即将进入页面的组件列表
	initComponentsList();
}

function openBrowser() {
	//提示打开浏览器
	// MessageBox.alert('请在浏览器中打开！')
	let u = navigator.userAgent;
	let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
	var Iosimgdiv = '<img class="Iosimgdiv" src="static/image/iOS.png"' +
		'style="width:100%;height:100%;">'
	var Androidimgdiv = '<img class="Androidimgdiv" src="static/image/Android.png"' +
		'style="width:100%;height:100%;">'
	let text = document.createElement("div");
	let textStyle = `width:100%;height:100%;position: fixed;top:0;z-index: 999999;`;
	text.setAttribute("style", textStyle);
	if(isIos) {
		text.innerHTML = Iosimgdiv;
		document.querySelector(".home-container").appendChild(text);
	} else if(isAndroid) {
		text.innerHTML = Androidimgdiv;
		document.querySelector(".home-container").appendChild(text);
	}
}

function createIframe() {
	//添加iframe
	let iframe = document.createElement('iframe');
	iframe.style.display = 'none';
	document.body.appendChild(iframe);
	return iframe;
}

function openApp() {
	let oldUrl = location.href.split('H5')[0] + 'download/index.html';
	let localUrl = "ningjiaxiansheng://ningjiaxiansheng.app.com";
	let openIframe = createIframe();
	let u = navigator.userAgent;
	let isIos = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
	let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
	let isChrome = window.navigator.userAgent.indexOf("Chrome") !== -1;
	if(isIos) {
		//判断是否是ios
		window.location.href = localUrl;
		let loadDateTime = Date.now();
		setTimeout(function() {
			let timeOutDateTime = Date.now();
			if(timeOutDateTime - loadDateTime < 2500) {
				window.location.href = oldUrl;
			}
		}, 2000);
	} else if(isAndroid) {
		//判断是否是android
		if(isChrome) {
			//chrome浏览器用iframe打不开得直接去打开，算一个坑
			window.location.href = localUrl;
		} else {
			//抛出你的scheme
			openIframe.src = localUrl;
		}
		setTimeout(function() {
			window.location.href = oldUrl;
		}, 500);
	} else {
		//主要是给winphone的用户准备的,实际都没测过，现在winphone不好找啊
		openIframe.src = localUrl;
		setTimeout(function() {
			window.location.href = oldUrl;
		}, 500);
	}
}